package com.lgy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgy.dao.CustomerDao;
import com.lgy.pojo.Customer;
import com.lgy.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public int deleteByPrimaryKey(Integer customerId) {
        return customerDao.deleteByPrimaryKey(customerId);
    }

    @Override
    public int insert(Customer record) {
        //根据客户id查询数据库,确定当前提交的姓名是否重复
        List<Customer> customerList = customerDao.findByCompanyId(record.getCompanyId());
        for (Customer customer : customerList) {
            if(customer.getCustomerPhone().equals(record.getCustomerPhone())){
                return 0;
            }
        }
        return customerDao.insert(record);
    }

    @Override
    public int insertSelective(Customer record) {
        return customerDao.insertSelective(record);
    }

    @Override
    public Customer selectByPrimaryKey(Integer customerId) {
        return customerDao.selectByPrimaryKey(customerId);
    }

    @Override
    public int updateByPrimaryKeySelective(Customer record) {
        return customerDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Customer record) {
        return customerDao.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo findAllById(Integer companyId,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Customer> customerList = customerDao.findAllById(companyId);
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(customerList);
        return pageInfo;
    }

    @Override
    public Customer findByPhone(String customerPhone,Integer companyId) {
        return customerDao.findByPhone(customerPhone,companyId);
    }
}
